
// 内容脚本监听扩展程序的消息
chrome.runtime.onMessage.addListener((response) => {
    // 内容脚本在网页运行,扩展程序常驻浏览器环境
    // 扩展程序和内容脚本间的通信使用消息传递的方式。两边均可以监听另一边发来的消息，
    // 并通过同样的通道回应。消息可以包含任何有效的 
    // JSON 对象（null、boolean、number、string、array 或 object）
    // console.log('收到来自FreeCopy扩展的消息',response)
    // 使能/禁用页面定义的选择文本，鼠标右键,复制文本事件
    new FreeCopy(response.isEnabled);
})

// const excludeGoogle=!/(\*+)?google(\*+)?/.test(location.host);
// // performance.navigation.type
// // 0-刚进入页面 1-页面刷新 2-页面回退 
// // 刷新页面后要重新监听所有与复制操作相关的事件
// if ([0,1].includes(performance.navigation.type)) {
//     // 内容脚本给扩展程序发消息
//     if(excludeGoogle){
//         console.log('content script enterpage sendMessage  host',location.host)
//         chrome.runtime.sendMessage({host:location.host});
//     }
// }


class FreeCopy {
    constructor(msg) {
        // console.log(typeof msg,msg)
        if (msg) {

            this.contextMenuTrigger();
            this.copyTrigger();
            this.pasteTrigger();
            this.cutTrigger();
            this.selectJSTrigger();
            this.selectCSSTrigger();
            this.mouseMoveTrigger();
            this.mouseOnTrigger();
            this.mouseDownTrigger();
            this.mouseUpTrigger();
        }
        else {
            this.contextMenuDefault();
            this.copyDefault();
            this.pasteDefault();
            this.cutDefault();
            this.selectJSDefault();
            this.selectCSSDefault();
            this.mouseMoveDefault();
            this.mouseOnDefault();
            this.mouseDownDefault();
            this.mouseUpDefault();
        }
    }

    contextMenuTrigger() {
        document.addEventListener('contextmenu', this.contextMenu, true);
    }

    contextMenuDefault() {
        document.removeEventListener('contextmenu', this.contextMenu, true);
    }

    contextMenu(e) {
        e.stopImmediatePropagation();
    }

    pasteTrigger() {
        document.addEventListener('paste', this.paste, true);
    }

    pasteDefault() {
        document.removeEventListener('paste', this.paste, true);
    }

    paste(e) {
        e.stopImmediatePropagation();
    }

    copyTrigger() {
        document.addEventListener('copy', this.copy, true);
    }

    copyDefault() {
        document.removeEventListener('copy', this.copy, true);
    }

    copy(e) {
        e.stopImmediatePropagation();
    }

    cutTrigger() {
        document.addEventListener('cut', this.cut, true);
    }

    cutDefault() {
        document.removeEventListener('cut', this.cut, true);
    }

    cut(e) {
        e.stopImmediatePropagation();
    }

    selectJSTrigger() {
        document.addEventListener('selectstart', this.selectJS, true);
    }

    selectJSDefault() {
        document.removeEventListener('selectstart', this.selectJS, true);
    }

    selectJS(e) {
        e.stopImmediatePropagation();
    }

    selectCSSTrigger() {
        document.addEventListener('mousedown', this.selectCSS, true);
    }

    selectCSSDefault() {
        document.removeEventListener('mousedown', this.selectCSS, true);
    }

    selectCSS(e) {
        // 鼠标按下时设置页面内容可选
        e.target.style.userSelect = "text";
    }

    mouseMoveTrigger() {
        document.addEventListener('mousemove', this.mouseMove, true);
    }

    mouseMoveDefault() {
        document.removeEventListener('mousemove', this.mouseMove, true);
    }

    mouseMove(e) {
        e.stopImmediatePropagation();
    }

    mouseOnTrigger() {
        document.addEventListener('mouseon', this.mouseOn, true);
    }

    mouseOnDefault() {
        document.removeEventListener('mouseon', this.mouseOn, true);
    }

    mouseOn(e) {
        e.stopImmediatePropagation();
    }

    mouseDownTrigger() {
        document.addEventListener('mousedown', this.mouseDown, true);
    }

    mouseDownDefault() {
        document.removeEventListener('mousedown', this.mouseDown, true);
    }

    mouseDown(e) {
        e.stopImmediatePropagation();

    }
    mouseUpTrigger() {
        document.addEventListener('mouseup', this.mouseUp, true);
    }

    mouseUpDefault() {
        document.removeEventListener('mouseup', this.mouseUp, true);
    }

    mouseUp(e) {
        e.stopImmediatePropagation();
    }


}

